﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DATA.BusinessObjects;

namespace Presentation
{
    public partial class frmTraCuuKhachHang : Form
    {
        public frmTraCuuKhachHang()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmTraCuuKhachHang_Load(object sender, EventArgs e)
        {
            IList<Loaikhach> list = LoaiKhachBUS.LayDanhSachLoaiKhach();

            DataGridViewComboBoxColumn dgvcbx = (DataGridViewComboBoxColumn)dgvDanhSachKhachHang.Columns["LoaiKhach"];
            dgvcbx.DataSource = list;
            dgvcbx.DisplayMember = "TenLoaiKhach";
            dgvcbx.ValueMember = "Id";

            list = LoaiKhachBUS.LayDanhSachLoaiKhach();

            Loaikhach lk = new Loaikhach();
            lk.Id = "0";
            lk.TenLoaiKhach = "---Chọn tất cả---";
            list.Insert(0, lk);

            cbxLoaiKhach.DataSource = list;
            cbxLoaiKhach.DisplayMember = "TenLoaiKhach";
            cbxLoaiKhach.ValueMember = "Id";
        }

        private void btnTraCuu_Click(object sender, EventArgs e)
        {
            Khachhang kh = new Khachhang();
            kh.TenKhachHang = txtTenKhachHang.Text;
            kh.Cmnd = txtCMND.Text;
            kh.DiaChi = txtDiaChi.Text;
            kh.Loaikhach = (Loaikhach)cbxLoaiKhach.SelectedItem;
            IList<Khachhang> list = KhachHangBUS.TimKiem(kh);
            dgvDanhSachKhachHang.DataSource = list;
        }

        private void dgvDanhSachKhachHang_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {            
            Loaikhach lk = (Loaikhach)dgvDanhSachKhachHang.Rows[dgvDanhSachKhachHang.Rows.Count - 1].Cells["Loaikhachs"].Value;
            if(lk != null)
            {
                dgvDanhSachKhachHang.Rows[dgvDanhSachKhachHang.Rows.Count - 1].Cells["LoaiKhach"].Value = lk.Id;
                dgvDanhSachKhachHang.Rows[dgvDanhSachKhachHang.Rows.Count - 1].Cells["STT"].Value = dgvDanhSachKhachHang.Rows.Count;
            }                
        }

        private void btn_CapNhatKhachHang_Click(object sender, EventArgs e)
        {
            try
            {
                int iRows = dgvDanhSachKhachHang.Rows.Count;
                if (iRows > 0)
                {
                    for (int i = 0; i < iRows - 1; i++)
                    {
                        string maKhachHang = "";
                        string tenKhachHang = "";
                        string tenLoaiKhach = "";
                        string CMND = "";
                        string diaChi = "";
                        maKhachHang = dgvDanhSachKhachHang.Rows[i].Cells["Id"].Value.ToString();
                        Khachhang khCapNhat = KhachHangBUS.LayKhachHangTheoMaKhachHang(maKhachHang);

                        if (dgvDanhSachKhachHang.Rows[i].Cells["TenKhachHang"].Value != null)
                        {
                            tenKhachHang = dgvDanhSachKhachHang.Rows[i].Cells["TenKhachHang"].Value.ToString();
                        }
                        if (dgvDanhSachKhachHang.Rows[i].Cells["LoaiKhach"].Value != null)
                        {
                            tenLoaiKhach = dgvDanhSachKhachHang.Rows[i].Cells["LoaiKhach"].Value.ToString();
                        }
                        if (dgvDanhSachKhachHang.Rows[i].Cells["CMND"].Value != null)
                        {
                            CMND = dgvDanhSachKhachHang.Rows[i].Cells["CMND"].Value.ToString();
                        }
                        if (dgvDanhSachKhachHang.Rows[i].Cells["DiaChi"].Value != null)
                        {
                            diaChi = dgvDanhSachKhachHang.Rows[i].Cells["DiaChi"].Value.ToString();
                        }

                        khCapNhat.TenKhachHang = tenKhachHang;
                        khCapNhat.Cmnd = CMND;
                        khCapNhat.DiaChi = diaChi;    // 0: còn trống | 1: có người thuê

                        Loaikhach loaiKhach = LoaiKhachBUS.LayLoaiKhachTheoID(tenLoaiKhach);
                        if (loaiKhach != null)
                        {
                            khCapNhat.Loaikhach = loaiKhach;
                        }

                        bool flag = KhachHangBUS.CapNhatKhachHang(khCapNhat) ;
                        if (flag == false)
                        {
                            MessageBox.Show("Không cập nhật được khách hàng: " + (i+1).ToString());
                        }
                    }
                    MessageBox.Show("Cập nhật thành công!");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Lỗi, không cập nhật được khách hàng", ex);
            }
        }
    }
}
